FROM node:18-alpine AS base
WORKDIR /app

# Copy all files
COPY web/personas-open-source .

# Install dependencies using npm install for potentially better compatibility in build
# Still using legacy-peer-deps based on potential project setup
RUN npm install --legacy-peer-deps && \
    npm install --save-dev @types/mixpanel-browser --legacy-peer-deps

# Environment variables should be passed at build time
ARG NEXT_PUBLIC_FIREBASE_API_KEY
ARG NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN
ARG NEXT_PUBLIC_FIREBASE_PROJECT_ID
ARG NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET
ARG NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID
ARG NEXT_PUBLIC_FIREBASE_APP_ID
ARG NEXT_PUBLIC_RAPIDAPI_KEY
ARG NEXT_PUBLIC_RAPIDAPI_HOST
ARG CLAUDE_API_KEY
ARG NEXT_PUBLIC_FIREBASE_VAPID_KEY
ARG OPENROUTER_API_KEY
ARG NEXT_PUBLIC_MIXPANEL_TOKEN
ARG NEXT_PUBLIC_EXTRA_PROMPT_RULES
ARG NEXT_PUBLIC_LINKEDIN_API_KEY
ARG NEXT_PUBLIC_LINKEDIN_API_HOST

ENV NEXT_PUBLIC_FIREBASE_API_KEY=$NEXT_PUBLIC_FIREBASE_API_KEY
ENV NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=$NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN
ENV NEXT_PUBLIC_FIREBASE_PROJECT_ID=$NEXT_PUBLIC_FIREBASE_PROJECT_ID
ENV NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=$NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET
ENV NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=$NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID
ENV NEXT_PUBLIC_FIREBASE_APP_ID=$NEXT_PUBLIC_FIREBASE_APP_ID
ENV NEXT_PUBLIC_RAPIDAPI_KEY=$NEXT_PUBLIC_RAPIDAPI_KEY
ENV NEXT_PUBLIC_RAPIDAPI_HOST=$NEXT_PUBLIC_RAPIDAPI_HOST
ENV CLAUDE_API_KEY=$CLAUDE_API_KEY
ENV NEXT_PUBLIC_FIREBASE_VAPID_KEY=$NEXT_PUBLIC_FIREBASE_VAPID_KEY
ENV OPENROUTER_API_KEY=$OPENROUTER_API_KEY
ENV NEXT_PUBLIC_MIXPANEL_TOKEN=$NEXT_PUBLIC_MIXPANEL_TOKEN
ENV NEXT_PUBLIC_EXTRA_PROMPT_RULES=$NEXT_PUBLIC_EXTRA_PROMPT_RULES
ENV NEXT_PUBLIC_LINKEDIN_API_KEY=$NEXT_PUBLIC_LINKEDIN_API_KEY
ENV NEXT_PUBLIC_LINKEDIN_API_HOST=$NEXT_PUBLIC_LINKEDIN_API_HOST

# Build the Next.js application
RUN npm run build

# Expose the port the app runs on
EXPOSE 3000

# Start the application
CMD ["npm", "start"]
